package cn.kow.kmall.controller.order;

import cn.kow.kmall.admin.pojo.dto.ServiceOrderAuditDTO;
import cn.kow.kmall.admin.pojo.vo.ServiceOrderVO;
import cn.kow.kmall.common.enums.OrderServiceStatusEnum;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.core.util.MoneyUtils;
import cn.kow.kmall.pojo.dto.OrderAfterSaleQueryDTO;
import cn.kow.kmall.pojo.entity.OrderAfterSale;
import cn.kow.kmall.service.OrderAfterSaleService;
import cn.kow.kmall.service.OrderRefundService;
import cn.kow.kmall.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-05-13 15:27
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/order/after-sale")
public class ServiceOrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderRefundService orderRefundService;

    @Autowired
    private OrderAfterSaleService orderAfterSaleService;

    /**
     * 列表
     *
     * @return
     */
    @PostMapping("/list")
    public KmallResult list(@RequestBody OrderAfterSaleQueryDTO queryDTO) {
        Page<OrderAfterSale> page = orderAfterSaleService.listByPage(queryDTO);
        List<ServiceOrderVO> voList = new ArrayList<>();
        if (page.hasContent()) {
            for (OrderAfterSale order : page.getContent()) {
                voList.add(toVO(order));
            }
        }
        return KmallResult.success().put("rows", voList)
                .put("total", page.getTotalElements());
    }

    /**
     * 详情
     *
     * @param serviceNo
     * @return
     */
    @GetMapping("/{serviceNo}")
    public KmallResult info(@PathVariable String serviceNo) {
        OrderAfterSale afterSale = orderAfterSaleService.getByServiceNo(serviceNo);
        if (null == afterSale) {
            return KmallResult.fail("服务单不存在");
        }
        ServiceOrderVO vo = new ServiceOrderVO();
        BeanUtils.copyProperties(afterSale, vo);
        vo.setApplyTime(afterSale.getCreateTime());
        return KmallResult.success(toVO(afterSale));
    }

    private ServiceOrderVO toVO(OrderAfterSale afterSale) {
        ServiceOrderVO vo = new ServiceOrderVO();
        BeanUtils.copyProperties(afterSale, vo);
        vo.setApplyTime(afterSale.getCreateTime());
        vo.setPayAmount(MoneyUtils.Fen2YuanStr(afterSale.getItemFee()));
        vo.setUnitPrice(MoneyUtils.Fen2YuanStr(afterSale.getUnitPrice()));
        return vo;
    }

    /**
     * 审核
     *
     * @param auditDTO
     * @return
     */
    @PostMapping("/audit")
    public KmallResult audit(@RequestBody ServiceOrderAuditDTO auditDTO) {
        List<String> serviceNoList = auditDTO.getServiceNos();
        for (String serviceNo : serviceNoList) {
            OrderAfterSale afterSale = orderAfterSaleService.getByServiceNo(serviceNo);
            if (null == afterSale) {
               continue;
            }
            if (!OrderServiceStatusEnum.UN_AUDIT.getValue().equals(afterSale.getServiceStatus())) {
                continue;
            }
            afterSale.setServiceStatus(OrderServiceStatusEnum.AUDIT_PASS.getValue());
            afterSale.setUpdateBy(currentUser().getUsername());
            orderAfterSaleService.update(afterSale);
        }
        return KmallResult.success();
    }

    /**
     * 审核拒绝
     *
     * @param auditDTO
     * @return
     */
    @PostMapping("/audit/reject")
    public KmallResult auditReject(@RequestBody ServiceOrderAuditDTO auditDTO) {
        List<String> serviceNoList = auditDTO.getServiceNos();
        for (String serviceNo : serviceNoList) {
            OrderAfterSale afterSale = orderAfterSaleService.getByServiceNo(serviceNo);
            if (null == afterSale) {
                continue;
            }
            if (!OrderServiceStatusEnum.UN_AUDIT.getValue().equals(afterSale.getServiceStatus())) {
                continue;
            }
            afterSale.setServiceStatus(OrderServiceStatusEnum.AUDIT_REJECT.getValue());
            afterSale.setRemark(auditDTO.getReason());
            afterSale.setUpdateBy(currentUser().getUsername());
            orderAfterSaleService.update(afterSale);
        }
        return KmallResult.success();
    }
}
